<?php
declare (strict_types=1);

namespace app\api\controller;

use app\model\ArticleModel;
use app\model\CategoryModel;
use app\model\UserModel;
use app\model\StaffJurisdictionModel;
use app\model\StaffRoleModel;
use app\model\CollectModel;
use app\model\ChargebackModel;
use think\facade\Request;
use think\facade\Validate;

class Article
{
    // 文章列表
    public function list()
    {
        $params = Request::param();
        $params['page'] = $params['page'] ?? 1;
        $params['page_size'] = $params['page_size'] ?? 15;
        $params['category_id']=$params['category_id'] ?? 0;
        $params['open_id']=$params['open_id'] ?? '';
        $validate = Validate::rule([
            'page' => 'between:1,' . PHP_INT_MAX,
            'page_size' => 'between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check($params)) {
            echo $validate->getError();
            exit();
        }

//        $articleList = ArticleModel::where('category_id', '=', 1)
//            ->order('add_time desc')
//            ->paginate(['list_rows' => 10]);
//        var_dump($articleList);exit();

        $model = ArticleModel::where('article_id', '>', 0);
            if($params['category_id']!=0){
               $model->where("category_id","=",$params['category_id']);
            };
            if($params['open_id']){
              $model->where("open_id","=",$params['open_id']);
            };
            $result=$model->order('add_time desc')
            ->paginate(['list_rows' => $params['page_size'], 'page' => $params['page']]);

        $articleList = [];
        foreach ($result as $item) {
            $category = CategoryModel::find($item['category_id']);
            $item['category_name'] = $category['category_name'];
            
            $User =UserModel::where("open_id","=",$item['open_id'])->find();
            $item['nickname'] = $User['nickname'];
            
            $commentLength =StaffJurisdictionModel::where("article_id","=",$item['article_id'])->count();
            $item['comment_length'] = $commentLength;
            
            $likeLength =StaffRoleModel::where("article_id","=",$item['article_id'])->where('like_status','=','1')->count();
            $item['like_length'] = $likeLength;
            
            $collectLength =CollectModel::where("article_id","=",$item['article_id'])->where('collect_status','=','1')->count();
            $item['collect_length'] = $collectLength;
            
            $share =ChargebackModel::where("article_id","=",$item['article_id'])->sum('share_count');
            $item['share_count'] = $share;
            
            $articleList[] = $item;
        }
        $data = [
            'status' => 0,
            'message' => '',
            'data' => [
                'articleList' => $articleList,
                'total' => $result->total()
            ],
        ];
        return json($data);
    }

    // 文章详情接口
    public function detail()
    {
        $articleId = Request::param("article_id");
        $validate = Validate::rule([
            'article_id|文章id' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['article_id' => $articleId])) {
            $data = [
                'status' => 10001,
                'message' => $validate->getError(),
                'data' => [],
            ];
            return json($data);
        }

        $article = ArticleModel::find($articleId);
        
        $User =UserModel::where("open_id","=",$article['open_id'])->find();
        $article['nickname'] = $User['nickname'];
        $article['avatar'] = $User['avatar'];
        
        $likeLength =StaffRoleModel::where("article_id","=",$article['article_id'])->where('like_status','=','1')->count();
        $article['like_length'] = $likeLength;
            
        $collectLength =CollectModel::where("article_id","=",$article['article_id'])->where('collect_status','=','1')->count();
        $article['collect_length'] = $collectLength;
        
        $share =ChargebackModel::where("article_id","=",$article['article_id'])->sum('share_count');
        $article['share_count'] = $share;
        
        if (!$article) {
            $data = [
                'status' => 11001,
                'message' => '文章不存在',
                'data' => [],
            ];
            return json($data);
        }
        $data = [
            'status' => 0,
            'message' => '',
            'data' => [
                'article' => $article,
            ],
        ];
        return json($data);
    }


}
